<template>
	<view class="content">
		<p class="title">选择合同类型</p>
		<view class="selectBox">
			<view class="selectTop" @click="show = !show">
				<span>{{selectType==0?'选择合同类型':textList[selectType-1].name}}</span>
				<image src="http://cdn.yic3.cn/lawyer/mp-xhs/contract/icon6.png" mode="widthFix"></image>
			</view>
			<view class="selectBottom" v-if="show">
				<p v-for="(item,i) in textList" :key="i" @click="changeSelect(item)">
					<text>{{item.name}}</text>
					<image v-show="item.id == selectType" src="http://cdn.yic3.cn/lawyer/mp-xhs/contract/icon9.png"
						mode="">
					</image>
				</p>
			</view>
		</view>
		<view class="fileUpdate">
			<view class="" @click="chooseFile">
				<image src="http://cdn.yic3.cn/lawyer/mp-xhs/contract/icon7.png" mode="widthFix"></image>
				<p>点击上传合同文件</p>
				<p>支持格式：pdf，大小不超过1M</p>
			</view>
		</view>
	</view>
</template>
<script>
	// #ifdef APP
	const testModule = uni.requireNativePlugin("iOSUploadPlugin-UploadModule")
	// #endif
	export default {
		data() {
			return {
				tabIndex: 0,
				list: [],
				show: false,
				textList: [{
					id: 1,
					name: '合同审查-甲方立场'
				}, {
					id: 2,
					name: '合同审查-乙方立场'
				}],
				selectType: 0,
			};
		},
		methods: {
			changeSelect(e) {
				this.selectType = e.id
				this.show = false
			},
			chooseFile() {
				if (this.selectType == 0) {
					uni.showToast({
						title: '请选择合同类型',
						icon: 'none'
					})
					this.show = true
					return
				}
				if (!this.isVip()) return uni.navigateTo({
					url: '/packageA/pages/vipCenter/vipCenter'
				})
				if (this.isBindPhone()) return false
				// #ifdef MP-WEIXIN
				this.wxUpload()
				// #endif
				// #ifdef APP
				this.uploadFileiOS()
				// #endif
			},
			wxUpload() {
				let that = this
				const fs = uni.getFileSystemManager()
				uni.showLoading()
				uni.chooseMessageFile({
					count: 1,
					type: 'file',
					size: 1024 * 1024,
					extension: ['.pdf'],
					success(res) {
						console.log(res)

						if (res.tempFiles[0].size >= 1024 * 1024) {
							uni.showToast({
								title: '文件大小不能超过1M',
								icon: "none"
							})
							return
						}
						fs.readFile({
							filePath: res.tempFiles[0].path,
							encoding: 'base64',
							success(base) {
								console.log(base)
								that.uploadFile(res.tempFiles[0].name, base.data)
							},
							fail(err) {
								uni.hideLoading()
								uni.showToast({
									title: '文件上传失败请重试',
									icon: 'none'
								})
							}
						})
					},
					fail() {
						uni.hideLoading()
						uni.showToast({
							title: '文件上传失败请重试',
							icon: 'none'
						})
					}
				})
			},
			uploadFileiOS() {
				// 参数1：数组 支持选择的文件类型
				// 参数2：是否支持多选
				// 参数3：异步回调
				uni.showLoading()
				testModule.openDocumentPickerWithTypes(
					["com.adobe.pdf"],
					false, 1024 * 1024, (ret) => {
						uni.hideLoading()
						try {
							let data = JSON.parse(ret)
							if (!data[0]) {
								uni.showToast({
									title: '文件大小不能超过1M',
									icon: 'none'
								})
								return
							}
							this.uploadFile(data[0].fileName, data[0].fileBase64)
						} catch (e) {
							uni.showToast({
								title: '文件大小不能超过1M',
								icon: 'none'
							})
							//TODO handle the exception
						}

					})
			},
			uploadFile(fileName, fileBase64) {
				console.log(fileName)
				this.$api.contractView({
					files: [{
						fileName,
						fileBase64,
					}],
					"fileType": "file",
					"contractType": this.selectType
				}).then(res => {
					uni.hideLoading()
					if (res.data.code == 0) {
						uni.navigateTo({
							url: '/packageA/pages/contractReview/contractPreview?type=1'
						})
					}
					console.log(res)
				})
			},




		}
	}
</script>

<style lang="scss">
	page {
		background-color: #15161C;
	}
</style>
<style lang="scss" scoped>
	@import 'contractReview.scss';
</style>